﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtletIS.Shared.Entities;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCheckPointsImport : Form
    {
        private DisciplineListEntity _discipline;
        private CheckPointsTableAdapter _taCheckPoints;

        public frmAdminCheckPointsImport(DisciplineListEntity discipline)
        {
            InitializeComponent();

            _discipline = discipline;
            _taCheckPoints = new CheckPointsTableAdapter();

            this.Icon = Properties.Resources.AtletIS64;
            this.lblTitle.Text = _discipline.Name;
        }

        private void frmAdminCheckPointsImport_Load(object sender, EventArgs e)
        {
            this.nudYear.Value = DateTime.Now.Year;
        }

        private void nudYear_ValueChanged(object sender, EventArgs e)
        {
            DatabaseHelper.FillCompetitionsByYear(this.aDbDataSet.Competitions, (int)this.nudYear.Value);
            this.cmbCompetitions_SelectedIndexChanged(null, null);
        }

        private void cmbCompetitions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbCompetitions.SelectedItem != null && this.cmbCompetitions.SelectedItem is DataRowView)
            {
                DataRowView selectedCompetition = (DataRowView)this.cmbCompetitions.SelectedItem;
                DatabaseHelper.FillDisciplineListByCompetitionId(this.aDbDataSet.DisciplineList, Convert.ToInt32(selectedCompetition["ID"]));
            }
        }

        private void tsbImport_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Naozaj chcete spustiť import pre " + _discipline.Name + " ?", "Import check-pointov", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                return;

            if (this.cmbDisciplineList.SelectedItem != null && this.cmbDisciplineList.SelectedItem is DataRowView)
            {
                DataRowView selectedDisciplineList = (DataRowView)this.cmbDisciplineList.SelectedItem;
                DataTable dt = DatabaseHelper.GetCheckPointsByDisciplineListId(Convert.ToInt32(selectedDisciplineList["ID"]));

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr.SetAdded();
                        dr["DisciplineListId"] = _discipline.ID;
                        dr["Active"] = 1;
                        dr["Sync"] = 0;
                        dr["WebId"] = 0;
                        dr["Modified"] = DateTime.Now;

                        _taCheckPoints.Update(dr);
                    }

                    MessageBox.Show("Import prebehol úspešne.", "Import check-pointov");
                }
            }
        }

        

        
    }
}
